package xyz.thoughtset.viewer.modules.ds.core.service;

import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.thoughtset.viewer.common.crud.core.service.BaseServiceImpl;
import xyz.thoughtset.viewer.modules.ds.core.dao.LinkerConfigDao;
import xyz.thoughtset.viewer.modules.ds.core.entity.LinkerConfig;

import java.util.LinkedHashMap;
import java.util.Objects;

@Service
@Transactional
public class LinkerConfigServiceImpl extends BaseServiceImpl<LinkerConfigDao, LinkerConfig> implements LinkerConfigService{

    @SneakyThrows
    @Override
    public Object saveData(LinkedHashMap<String, Object> baseMap) {
        String settingsKey = "otherSettings";
        Object settings = baseMap.remove(settingsKey);
        LinkerConfig o = convertValue(baseMap);
        if (Objects.nonNull(settings)){
            o.setOtherSettings(mapper.writeValueAsString(settings));
        }
        saveOrUpdate(o);
        return o;
    }

}
